/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.integral.core.dal.repository.impl;

import com.fulihui.integral.core.dal.dataobj.ISAccountInfo;
import com.fulihui.integral.core.dal.dataobj.ISAccountInfoExample;
import com.fulihui.integral.core.dal.mapper.ISAccountInfoMapper;
import com.fulihui.integral.core.dal.repository.ISBaseQueryRepository;
import com.fulihui.integral.core.dto.ISAccountInfoDTO;
import com.fulihui.integral.core.enums.ISAccountType;
import com.fulihui.integral.core.request.ISAccountInfoQueryReq;
import org.near.toolkit.common.StringUtil;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.inject.Inject;
import java.util.List;

/**
 * @author yunfeng.li
 * @version $Id: v 0.1 2017年04月14日 15:40 yunfeng.li Exp $
 */
@Repository
public class ISBaseQueryRepositoryImpl implements ISBaseQueryRepository {

    @Autowired
    private ISAccountInfoMapper iSAccountInfoMapper;

    /**
     * 查询个人积分余额
     *
     * @param req
     * @return
     */
    @Override
    public ISAccountInfoDTO queryISAccountInfo(ISAccountInfoQueryReq req) {
        ISAccountInfoExample selectExample = new ISAccountInfoExample();
        ISAccountInfoExample.Criteria criteria = selectExample.createCriteria();
        criteria.andUserIdEqualTo(req.getUserId());
        criteria.andIsAccountTypeEqualTo(StringUtil.isBlank(req.getIsAccountType())
                ? ISAccountType.BASE_JIFEN.getCode() : req.getIsAccountType());
        List<ISAccountInfo> isAccountInfos = iSAccountInfoMapper.selectByExample(selectExample);
        ISAccountInfoDTO dto = new ISAccountInfoDTO();
        if (!CollectionUtils.isEmpty(isAccountInfos) && isAccountInfos.size() == 1) {
            ISAccountInfo info = isAccountInfos.get(0);
            dto.setAmount(info.getAmount());
            dto.setIsAccountType(info.getIsAccountType());
            dto.setUserId(info.getUserId());
        }
        return dto;
    }

}
